package com.c.ctgproject.wiget.common

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.c.ctgproject.R
import com.c.ctgproject.utils.handlerNull

class AddAndSubLayout : LinearLayoutCompat {
    private var addBTN: RelativeLayout? = null
    private var subBTN: RelativeLayout? = null
    private var countTV: TextView? = null
    constructor(context: Context) : super(context)


    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)



    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_add_and_sub_layout, this)
        addBTN = view.findViewById(R.id.mAddBTN)
        subBTN = view.findViewById(R.id.mMinusBTN)
        countTV = view.findViewById(R.id.mCountTV)
        addBTN?.setOnClickListener {
            "${countTV?.text.handlerNull().toInt()+1}".also { countTV?.text = it }
        }
        subBTN?.setOnClickListener {
            if (countTV?.text!="0"){
                countTV?.text="${countTV?.text.handlerNull().toInt()-1}"
            }
        }
    }

    fun getCount(): Int {
        return countTV?.text.handlerNull().toInt()
    }

    fun setCount(count: String) {
        countTV?.text = count
    }


}